package com.czbank.financing.utils;

import com.sun.mail.util.MailSSLSocketFactory;
import org.junit.jupiter.api.Test;

import java.util.Properties;
import java.util.Random;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
 * @Auther: thinkpad
 * @Date: 2019/7/16 23:57
 * @Description:
 */
/**
 * 目前发送到163 qq个人邮箱可以，企业邮箱有问题
 * @param subject 邮件主题
 * @param text 邮件内容纯文本
 * @param mails 邮件地址多个用逗号隔开
 * @throws Exception
 */
public class QQSendEmial {

    public static void main(String[] args) throws Exception {
        Random random = new Random();
        String str =new String();
        char[] ch = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                'j', 'k','l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        for (int i = 0; i <6; i++){
            char num = ch[random.nextInt(ch.length)];
            str += num;
        }
        System.out.println("str = " + str);
        send_email("sb",str,"756921868@qq.com");
    }

    public static void send_email(String subject,String text,String mails) throws Exception{
        //用于读取配置文件
        Properties props=new Properties();
        //开启Debug调试
        props.setProperty("mail.debug", "true");
        //发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        //发送邮件服务器的主机名
        props.setProperty("mail.smtp.host", "smtp.qq.com");
        //端口号
        props.setProperty("mail.smtp.port", "465");
        //发送邮件协议
        props.setProperty("mail.transport.protocol", "smtp");
        //开启ssl加密（并不是所有的邮箱服务器都需要，但是qq邮箱服务器是必须的）
        MailSSLSocketFactory msf= new MailSSLSocketFactory();
        msf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory",msf);
        //获取Session会话实例（javamail Session与HttpSession的区别是Javamail的Session只是配置信息的集合）
        Session session= Session.getInstance(props,new javax.mail.Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication(){
                //用户名密码验证（取得的授权吗）
                return new PasswordAuthentication ("962433037@qq.com","nztojdcmpjqfbfge");
            }
        });

        //抽象类MimeMessage为实现类 消息载体封装了邮件的所有消息
        Message message=new MimeMessage(session);
        //设置邮件主题
        message.setSubject(subject);
        //封装需要发送电子邮件的信息
        message.setText(text);
        //设置发件人地址
        message.setFrom(new InternetAddress("962433037@qq.com"));
        //此类的功能是发送邮件 又会话获得实例
        Transport transport=session.getTransport();
        //开启连接
        transport.connect();
        //设置收件人地址邮件信息
        String mailAddress[] = mails.split(",");
        for (int i = 0; i < mailAddress.length; i++) {
            transport.sendMessage(message,new Address[]{new InternetAddress(mailAddress[i])});
            //邮件发送后关闭信息
            transport.close();
        }

    }
}
